#include "database.h"

database::database(){
    this -> db = QSqlDatabase::addDatabase("QMYSQL");
//    this -> db.setHostName("localhost");
//    this->db.setPort(3306);  //设置端口28857
//    this -> db.setDatabaseName("car");//parking
//    this -> db.setUserName("root");
//    this -> db.setPassword("xf20000530");
    //链接
        //db.setHostName("sh-cynosdbmysql-grp-duhnjgm2.sql.tencentcdb.com");
        //db.setPort(28857);
        db.setHostName("localhost");
        db.setPort(3306);
        db.setUserName("root");
        db.setPassword("chenkai");
        db.setDatabaseName("parking");
    bool conn = this -> db.open();
    //QSqlQuery sql_query(db);
    if (!conn) {
        QMessageBox msgBox;
        msgBox.setText(QObject::tr("无法连接数据库,请检查数据库是否启动,数据库服务器地址是否正确!"));
        msgBox.exec();
    }
    else{
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("数据库成功连接！"), QMessageBox::Yes);
    }
}

database::~database(){
    this -> db.close();
}
